package com.lkx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.common.CustomException;
import com.lkx.common.R;
import com.lkx.mapper.CategoryMapper;
import com.lkx.mapper.EmployeeMapper;
import com.lkx.pojo.Category;
import com.lkx.pojo.Dish;
import com.lkx.pojo.Employee;
import com.lkx.pojo.Setmeal;
import com.lkx.service.CategoryService;
import com.lkx.service.DishService;
import com.lkx.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

/**
 * @ClassName: com.lkx.service.impl.CategoryServiceImpl
 * @author: likaixin
 * @create: 2022年11月22日 15:40
 * @desc:
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;
    //根据id删除分类，删除之前要判断当前类品是否关联了相连套餐
    @Override
    public void remove(long id) {
        //查询当前分类是否关联了菜品，如果已关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId,id);
        int count = dishService.count(lqw1);
        if (count>0){
            //已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除！");
        }
        //查寻当前分类是否关联了套餐，如果已关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setMealService.count(lqw2);
        if (countSetmeal>0){
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前套餐下关联了菜品，不能删除！");
        }
        super.removeById(id);
    }

}
